package com.xfp.aop;


import com.fasterxml.jackson.databind.JsonSerializable;
import com.xfp.anntation.AutoFill;
import com.xfp.context.BaseContext;
import com.xfp.enumclass.OperationClass;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Slf4j
@Aspect
@Component
public class AutoFillAspect {
    @Before("execution(* com.xfp.mapper.*.*(..))&& @annotation(com.xfp.anntation.AutoFill)")
    public void AutoFillTimeAndName(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        //获取标签对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取标签
        Method method = signature.getMethod();
        //获取注解名
        AutoFill annotation = method.getAnnotation(AutoFill.class);
        //获取注解类型
        OperationClass tp = annotation.value();
        //获取执行对象
        Object arg = joinPoint.getArgs()[0];
        //获取当前时间
        LocalDateTime time = LocalDateTime.now();
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);

        if(OperationClass.INSERT.equals(tp)){
            log.info("新增填充开始......");
            Method setCreateTime = arg.getClass().getMethod("setCreateTime", LocalDateTime.class);
            Method setUpdateTime = arg.getClass().getMethod("setUpdateTime", LocalDateTime.class);
            setCreateTime.invoke(arg,time);
            setUpdateTime.invoke(arg,time);
            try {
                Method setCreateUser = arg.getClass().getMethod("setCreateUser",Long.class);
                Method setUpdateUser = arg.getClass().getMethod("setUpdateUser",Long.class);
                setCreateUser.invoke(arg,currentId);
                setUpdateUser.invoke(arg,currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            log.info("更新填充启动......");
            Method setUpdateTime = arg.getClass().getMethod("setUpdateTime",LocalDateTime.class);
            setUpdateTime.invoke(arg,time);
            try{
                Method setUpdateUser = arg.getClass().getMethod("setUpdateUser",Long.class);
                setUpdateUser.invoke(arg,currentId);
            } catch (Exception e){
                throw new RuntimeException(e);
            }
        }


    }

    @After("execution(* com.xfp.mapper.*.*(..))&& @annotation(com.xfp.anntation.AutoFill)")
    public void removeBaseContext(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        BaseContext.removeCurrentId();
    }

}
